package com.ming.dancing.springboot.service;

import com.ming.dancing.springboot.Dao.DancerDao;
import com.ming.dancing.springboot.entity.Dancer;
import com.ming.dancing.springboot.entity.DancerQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DancerServiceImpl implements DancerService {

    @Autowired
    DancerDao dancerDao;
    @Override
    //查找所有舞者
    public List<Dancer> findallDancers() {
        return dancerDao.findallDancers();
    }

    @Override
    //增加舞者
    public Dancer adddancer(Dancer dancer) {
        return dancerDao.save(dancer);
    }

    @Override
    public Page<Dancer> findAllStudent(int pageIndex, int pageSize) {
        Pageable pages = PageRequest.of(pageIndex,pageSize);
        return dancerDao.findAll(pages);
    }

    @Override
    //查找舞者byid
    public Dancer findDancerById(Integer id) {
        return dancerDao.findById(id).get();
    }

    @Override
    //更新舞者
    public Dancer UpdateDancer(Dancer dancer) {
        return dancerDao.save(dancer);
    }

    @Override
    //删除舞者
    public void DeleteDancer(Integer id) {
        dancerDao.deleteById(id);
    }

    @Override
    //设置照片
    public void setpicture(Integer id, String newFileName) {
        dancerDao.setpicture(id,newFileName);
    }

    //分页查询
    //带查询条件的分页查询
    @Override
    public Page<Dancer> findDancerCriteria(Integer page, Integer size, DancerQuery dancerQuery) {
        Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC, "id");
        Page<Dancer> dancerPage=dancerDao.findAll(new Specification<Dancer>() {
            @Override
            public Predicate toPredicate(Root<Dancer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list=  new ArrayList<Predicate>();
                if(null!=dancerQuery.getDancername()&&!"".equals(dancerQuery.getDancername()))
                {
                    list.add(criteriaBuilder.equal(root.get("dancername").as(String.class), dancerQuery.getDancername()));
                }
                if(null!=dancerQuery.getSex()&&!"".equals(dancerQuery.getSex()))
                {
                    list.add(criteriaBuilder.equal(root.get("sex").as(String.class), dancerQuery.getSex()));
                }
                if(null!=dancerQuery.getType()&&!"".equals(dancerQuery.getType()))
                {
                    list.add(criteriaBuilder.equal(root.get("type").as(String.class), dancerQuery.getType()));
                }
                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));

            }
        },pageable);
        return dancerPage;

    }


}
